
"use strict";

const Service = require("egg").Service;
const http = require('../extend/curl')
const querystring = require('querystring');
class BaiDuApiService extends Service {
  // 获取baiduai上传access_token
  async getStsToken(body) {
    try {
      console.log(body)
      const param = querystring.stringify({
        'grant_type': 'client_credentials',
        'client_id': body.client_id,
        'client_secret': body.client_secret
      });

      const url = 'https://aip.baidubce.com/oauth/2.0/token?' + param;

      const data = await http.post(url, body, this.ctx)

      return this.ctx.helper.ormSuccess({
        refresh_token: data.refresh_token,
        expires_in: data.expires_in,
        session_key: data.session_key,
        access_token: data.access_token,
        scope: data.scope,
        session_secret: data.session_secret,
      })
    } catch (error) {
      return this.ctx.helper.ormError(err);
    }
  }



  /**
   * 图像分类
   * @param {参数类型} 参数名 - 参数描述
   * @returns {返回值类型} 返回值描述
   */
  async picCategory(body, access_token) {
    try {
      const param = querystring.stringify({
        'input_type': body.input_type,
        access_token
      });
      delete body.input_type
      //这里根据接口的不同修改，比如我自己的是第一个，博是第二个。
//      const url = 'https://aip.baidubce.com/rpc/2.0/ai_custom/v1/classification/fruitSelection?' + param;
      const url = 'https://aip.baidubce.com/rpc/2.0/ai_custom/v1/classification/crops81?' + param;
      console.log(body)
      const data = await http.post(url, body, this.ctx)

      return this.ctx.helper.ormSuccess(data)
    } catch (err) {
      return this.ctx.helper.ormError(err);
    }
  }



  /**
   * 图像集列表
   * @param {参数类型} 参数名 - 参数描述
   * @returns {返回值类型} 返回值描述
   */
  async list_datasets(body, access_token) {
    try {
      console.log(body)
      console.log(access_token)
      const param = querystring.stringify({
        access_token
      });
      const url = 'https://aip.baidubce.com/rpc/2.0/easydl/dataset/list?' + param;

      const data = await http.post(url, body, this.ctx)
      
      return this.ctx.helper.ormSuccess(data)
    } catch (err) {
      return this.ctx.helper.ormError(err);
    }
  }



  /**
   * 创建图像集
   * @param {参数类型} 参数名 - 参数描述
   * @returns {返回值类型} 返回值描述
   */
  async create_dataset(body,access_token) {
    try {
      console.log(body)
      console.log(access_token)
      const param = querystring.stringify({
        access_token
      });
      const url = 'https://aip.baidubce.com/rpc/2.0/easydl/dataset/create?' + param;

      const data = await http.post(url, body, this.ctx)
      
      return this.ctx.helper.ormSuccess(data)
    } catch (err) {
      return this.ctx.helper.ormError(err);
    }
  }



  /**
   * 删除图像集
   * @param {参数类型} 参数名 - 参数描述
   * @returns {返回值类型} 返回值描述
   */
  async delete_dataset(body,access_token) {
    try {
      console.log(body)
      console.log(access_token)
      const param = querystring.stringify({
        access_token
      });
      const url = 'https://aip.baidubce.com/rpc/2.0/easydl/dataset/delete?' + param;

      const data = await http.post(url, body, this.ctx)
      
      return this.ctx.helper.ormSuccess(data)
    } catch (err) {
      return this.ctx.helper.ormError(err);
    }

  }



  /**
   * 标签列表
   * @param {参数类型} 参数名 - 参数描述
   * @returns {返回值类型} 返回值描述
   */
  async list_categories(body,access_token) {
    try {
      console.log(body)
      console.log(access_token)
      const param = querystring.stringify({
        access_token
      });
      const url = 'https://aip.baidubce.com/rpc/2.0/easydl/label/list?' + param;

      const data = await http.post(url, body, this.ctx)
      
      return this.ctx.helper.ormSuccess(data)
    } catch (err) {
      return this.ctx.helper.ormError(err);
    }

  }


  /**
   * 删除标签
   * @param {参数类型} 参数名 - 参数描述
   * @returns {返回值类型} 返回值描述
   */
  async delete_category(body,access_token) {
    try {
      console.log(body)
      console.log(access_token)
      const param = querystring.stringify({
        access_token
      });
      const url = 'https://aip.baidubce.com/rpc/2.0/easydl/label/delete?' + param;

      const data = await http.post(url, body, this.ctx)
      
      return this.ctx.helper.ormSuccess(data)
    } catch (err) {
      return this.ctx.helper.ormError(err);
    }
  }



  /**
   * 添加数据
   * @param {参数类型} 参数名 - 参数描述
   * @returns {返回值类型} 返回值描述
   */
  async add_data(body,access_token) {
    try {
      console.log(body)
      console.log(access_token)
      const param = querystring.stringify({
        access_token
      });
      const url = 'https://aip.baidubce.com/rpc/2.0/easydl/dataset/addentity?' + param;

      const data = await http.post(url, body, this.ctx)
      
      return this.ctx.helper.ormSuccess(data)
    } catch (err) {
      return this.ctx.helper.ormError(err);
    }
  }
}

module.exports = BaiDuApiService;
